/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Inscription.java
 *
 * Created on 5 janv. 2009, 01:53:32
 */

package fr.miage.shopping.presentation;

import fr.miage.shopping.metier.entite.Admin;
import fr.miage.shopping.metier.entite.Role;
import fr.miage.shopping.metier.impl.AdminBusinessServiceImpl;
import fr.miage.shopping.metier.impl.RoleBusinessServiceImpl;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author romain
 */
public class CreationAdmin extends javax.swing.JFrame {

    /** Creates new form Inscription */
    public CreationAdmin() {
        initComponents();
        chargerCombo();
    }
    /**
     * chargement de la combo box
     */
    private void chargerCombo() {
        RoleBusinessServiceImpl bs = new RoleBusinessServiceImpl();
        List<Role> laListe = bs.getAllRole();
        for(int i=0;i<laListe.size();i++){
            jComboBox1.addItem(laListe.get(i).getRole());
          
        }
    }

   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lLogin = new javax.swing.JLabel();
        tLogin = new javax.swing.JTextField();
        lEmail = new javax.swing.JLabel();
        tEmail = new javax.swing.JTextField();
        lPass = new javax.swing.JLabel();
        lPass2 = new javax.swing.JLabel();
        bValider = new javax.swing.JButton();
        bAnnuler = new javax.swing.JButton();
        tPass = new javax.swing.JPasswordField();
        tPass2 = new javax.swing.JPasswordField();
        lRole = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ajout d'un administrateur");
        setName("Inscription"); // NOI18N

        lLogin.setText("Login : ");

        lEmail.setText("Email :");

        lPass.setText("Mot de passe :");

        lPass2.setText("Confirmation mot de passe :");

        bValider.setText("Valider");
        bValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bValiderActionPerformed(evt);
            }
        });

        bAnnuler.setText("Annuler");
        bAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAnnulerActionPerformed(evt);
            }
        });

        tPass.setText("jPasswordField1");
        tPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tPassActionPerformed(evt);
            }
        });

        tPass2.setText("jPasswordField2");

        lRole.setText("Rôle :");

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                                .addGap(40, 40, 40))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lPass, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lPass2, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                                .addGap(42, 42, 42))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lRole)
                                .addGap(183, 183, 183)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tPass, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tPass2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(10, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bValider)
                        .addGap(42, 42, 42)
                        .addComponent(bAnnuler)
                        .addGap(90, 90, 90))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBox1, tEmail, tLogin, tPass, tPass2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                            .addComponent(tEmail)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tLogin)
                        .addGap(42, 42, 42)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lPass, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(tPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lPass2, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(tPass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lRole)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAnnuler)
                    .addComponent(bValider))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     *  Bouton de validation de création de client
     * @param evt
     */
    private void bValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bValiderActionPerformed
        if (erreurForm()){
            AdminBusinessServiceImpl bs = new AdminBusinessServiceImpl();
            Admin admin = new Admin();
            admin.setLogin(tLogin.getText());
            admin.setEmail(tEmail.getText());
            admin.setPassword(tPass.getText());
            
            RoleBusinessServiceImpl rbs = new RoleBusinessServiceImpl();
            List<Role>lesRoles = rbs.getAllRole();
            for(int i=0;i<lesRoles.size();i++){
                if(lesRoles.get(i).getRole().equals(jComboBox1.getSelectedItem().toString())){
                    admin.setRole(lesRoles.get(i));
                }
            }

            bs.createAdmin(admin);
            this.setVisible(false);
        }
}//GEN-LAST:event_bValiderActionPerformed

    private void bAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAnnulerActionPerformed
       this.setVisible(false);
    }//GEN-LAST:event_bAnnulerActionPerformed

    private void tPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tPassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tPassActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAnnuler;
    private javax.swing.JButton bValider;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel lEmail;
    private javax.swing.JLabel lLogin;
    private javax.swing.JLabel lPass;
    private javax.swing.JLabel lPass2;
    private javax.swing.JLabel lRole;
    private javax.swing.JTextField tEmail;
    private javax.swing.JTextField tLogin;
    private javax.swing.JPasswordField tPass;
    private javax.swing.JPasswordField tPass2;
    // End of variables declaration//GEN-END:variables

    /**
     * Test du formulaire
     * @return
     */
     private boolean erreurForm() {
        boolean noError = true;
         String msgErreur = "Le(s) champ(s) suivant(s) est/sont incorrectes :\n";
        if ((this.tEmail == null) || (this.tEmail.getText().length()<5) ||
                ((this.tEmail.getText().indexOf("@")==-1))){
            msgErreur+=" - Email\n";
            noError = false;
        }

        if ((this.tLogin == null) || (this.tLogin.getText().length()<1)){
            msgErreur+=" - Login\n";
            noError = false;
        }

        if ((!this.tPass.getText().equals(this.tPass2.getText()))){
             msgErreur+=" - Mot de passe\n";
            noError = false;
        }

        if ((this.tPass == null) || (this.tPass.getText().length()<1)){
            msgErreur+=" - Mot de passe\n";
            noError = false;
        }

        if ((this.tPass2 == null) || (this.tPass2.getText().length()<1)){
            msgErreur+=" - Adresse\n";
            noError = false;
        }
         if(!noError){
            JOptionPane.showMessageDialog(null,msgErreur,"Erreur formulaire",JOptionPane.WARNING_MESSAGE);
        }
        return noError;
    }


}
